<?php
/*  	shipping_id
		invoice_number
		cost
*/
function reequipment_user_pm()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:delete', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('model_id', 'int:1:', $model_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('area_id', 'int:1:', $area_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('schedule_id', 'int:1:', $schedule_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('maintenance_id', 'int:1:', $maintenance_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('equipment_id', 'int:1:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;

    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();
    xarTplSetPageTitle(xarML('Schedule PM'));
    // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');

    switch(strtolower($phase)) {
        case 'form':

            $data['pms'] = xarModAPIFunc('reequipment', 'user', 'getschedule', array('model_id' => $model_id, 'area_id' => $area_id, 'or' => true));
            foreach ($data['pms'] as $key => $var){
            	if ($var['downtime'] == 1){
            		$data['pms'][$key]['style'] = true;
            	} else {
            		$data['pms'][$key]['style'] = '';
            	}
            	// TODO fix this because it loops through the loop.
            	$maint = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('schedule_id' => $var['schedule_id'], 'equipment_id' => $equipment_id, 'company_id' => $company_id,  'date_complete' => true));
            	// return var_export($maint);
            	foreach ($maint as $m){
					if (isset($m['maintenance_id'])){
						$data['pms'][$key]['maintenance_id'] = $m['maintenance_id'];
						$data['pms'][$key]['rel_frequency_id'] = $m['rel_frequency_id'];
					} else {
						$data['pms'][$key]['maintenance_id'] = false;
						$data['pms'][$key]['rel_frequency_id'] = false;
					}
				}
				$data['pms'][$key]['model_id'] = $model_id;
				$data['pms'][$key]['area_id'] = $area_id;
            }
			$model_name = xarModAPIFunc('reequipment', 'user', 'getmodels', array('model_id' => $model_id));
			foreach ($model_name as $var){
				$data['model_name'] = $var['model_name'];
				$data['model_id'] = $var['model_id'];
				$data['area_id'] = $var['area_id'];
			}
			$data['equipment_id'] = $equipment_id;
          	// return var_export($data['pms']);


        break;

        case 'create':
        		if (empty($schedule_id)) {
        			xarResponseRedirect(xarModURL('reequipment', 'user', 'main'));
        		}

        		// We need the schedule information to fill the maintenance table.
        		$schedule = xarModAPIFunc('reequipment', 'user', 'getschedule', array('schedule_id' => $schedule_id));
				foreach($schedule as $var){
					$frequency = $var['frequency'];
					$issue = $var['issue'];
					$schedule_id = $var['schedule_id'];
				}
        		// return var_export($schedule);
        		// We also need to get the equipment ids that are with this model, so we can create an entity for each one.
        		// Doing all machines now.
        		$equipment = xarModAPIFunc('reequipment', 'user', 'getequipment', array('model_id' => $model_id, 'company_id' => $company_id));
        		// return var_export($equipment);
        		foreach ($equipment as $var){
        			// Make sure this maintenance isn't already scheduled
        			$check = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('schedule_id' => $schedule_id,  'equipment_id' => intval($var['equipment_id']), 'company_id' => $company_id));
					if ($check == false) {
						// We need to decide when the maintenance is due.  Just set it for two weeks from now, so we can do the initial one.  We still need to check in case this is cycles.
						$due = xarModAPIFunc('reequipment', 'user', 'determinefrequency', array('frequency' => $frequency, 'cycles' => $var['cycles']));
						// return var_export($var['equipment_id']);
						// return var_export($due);
						if (!empty($due['date'])){
							// return var_export($due['date']);
							$due['date'] = strtotime('+2 Week');
							$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('schedule_id' => $schedule_id, 'issue' => $issue, 'equipment_id' => intval($var['equipment_id']), 'company_id' => $company_id, 'date_due' => $due['date'], 'frequency_id' => 0));
						} elseif (!empty($due['cycles'])) {
							$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('schedule_id' => $schedule_id, 'issue' => $issue, 'equipment_id' => intval($var['equipment_id']), 'company_id' => $company_id, 'cycles_due' => $due['cycles'], 'frequency_id' => 0));
						}
					}
        		}

        		// We are doing this via ajax now
        		$get_maintenance = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('schedule_id' => $schedule_id,  'equipment_id' => $equipment_id, 'company_id' => $company_id));
        		//return var_export($get_maintenance);
                // xarResponseRedirect(xarModURL('reequipment', 'user', 'pm', array('model_id' => $model_id, 'area_id' => $area_id, 'equipment_id' => $equipment_id)));
                $data = xarTplModule('reequipment', 'ajax', 'pm_delete', array('maintenance_id' => $get_maintenance[0]['maintenance_id'], 'model_id' => $model_id, 'area_id' => $area_id, 'equipment_id' => $equipment_id, 'schedule_id' => $schedule_id));
        break;

        case 'delete':
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deletemaintenance', array( 'company_id' => $company_id, 'maintenance_id' => $maintenance_id, 'date_complete' => true));
                // We are doing this via ajax now
                // xarResponseRedirect(xarModURL('reequipment', 'user', 'pm', array('model_id' => $model_id, 'area_id' => $area_id, 'equipment_id' => $equipment_id)));
                $data = xarTplModule('reequipment', 'ajax', 'pm_create', array('model_id' => $model_id, 'area_id' => $area_id, 'equipment_id' => $equipment_id, 'schedule_id' => $schedule_id));
        break;

    }
    // Return the output
    return $data;
}
?>